package com.ruoyi.jimu.controller;

import com.ruoyi.clap.domain.XlkjClap;
import com.ruoyi.clap.service.IXlkjClapService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.jimu.entity.HouseServeyVo;
import com.ruoyi.jimu.entity.PopCountVo;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.utils.DictStrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 大数据驾驶舱接口
 */

@RestController
@RequestMapping("/app/jimu")
public class JimuController extends BaseController {

    @Autowired
    private IXlkjPopulationService populationService;
    @Autowired
    private IXlkjHouseService houseService;
    @Autowired
    private IXlkjCompanyService companyService;
    @Autowired
    private IXlkjIniService iniService;
    @Autowired
    private IXlkjClapService clapService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 总人口数量
     */
    @GetMapping("/popCount")
    public HouseServeyVo popCount(){
        List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(new XlkjPopulation());
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjPopulations.size());
        return serveyVo;
    }

    /**
     * 总房屋数量
     */
    @GetMapping("/houseCount")
    public HouseServeyVo houseCount(){
        List<XlkjHouse> xlkjPopulations = houseService.selectXlkjHouseList(new XlkjHouse());
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjPopulations.size());
        return serveyVo;
    }

    /**
     * 总单位数量
     */
    @GetMapping("/companyCount")
    public HouseServeyVo companyCount(){
        List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(new XlkjCompany());
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjCompanies.size());
        return serveyVo;
    }

    /**
     * 总上报事件数量
     */
    @GetMapping("/iniCount")
    public HouseServeyVo iniCount(){
        List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(new XlkjIni());
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjInis.size());
        return serveyVo;
    }

    /**
     * 总完成事件数量
     */
    @GetMapping("/iniSuccCount")
    public HouseServeyVo iniSuccCount(){
        XlkjIni ini = new XlkjIni();
        ini.setStatus("2");
        List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjInis.size());
        return serveyVo;
    }

    /**
     * 总随手拍上报事件
     */
    @GetMapping("/getClapCountNum")
    public HouseServeyVo clapCountNum(){
        XlkjClap clap = new XlkjClap();
        List<XlkjClap> xlkjClaps = clapService.selectXlkjClapList(clap);
        HouseServeyVo serveyVo = new HouseServeyVo();
        serveyVo.setValue(xlkjClaps.size());
        return serveyVo;
    }

    /**
     * 各部门人口采集统计
     * @return
     */
    @GetMapping("/getPopCount")
    public AjaxResult getPopCount(){
        List<PopCountVo> popCountVos = new ArrayList<>();

        List<SysDept> sysDepts = sysDeptService.selectDeptByParentId(100L);
        for(SysDept dept:sysDepts){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(dept.getDeptId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            XlkjPopulation population = new XlkjPopulation();
            population.setDepartmentList(str);
            List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(population);
            PopCountVo countVo = new PopCountVo();
            countVo.setName(dept.getDeptName());
            countVo.setValue(xlkjPopulations.size());
            countVo.setType("人口采集数");
            popCountVos.add(countVo);
        }
        return AjaxResult.success(popCountVos);
    }

    /**
     * 房屋统计概况（房屋户型）
     */
    @GetMapping("/getHouseSurvey")
    public AjaxResult getPopSurvet(){
        List<HouseServeyVo> houseServeyVos = new ArrayList<>();

        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(DictStrUtils.APARTMENT_TYPE);
        for(SysDictData data:sysDictData){
            XlkjHouse house = new XlkjHouse();
            house.setApartment(data.getDictValue());
            List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(house);
            HouseServeyVo houseServeyVo = new HouseServeyVo();
            houseServeyVo.setName(data.getDictLabel());
            houseServeyVo.setValue(xlkjHouses.size());
            houseServeyVos.add(houseServeyVo);
        }

        return AjaxResult.success(houseServeyVos);
    }

    /**
     * 房屋统计概况（房屋类型）
     */
    @GetMapping("/getHouseSurveyType")
    public AjaxResult getPopServeyType(){
        List<HouseServeyVo> houseServeyVos = new ArrayList<>();

        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(DictStrUtils.HOUSE_TYPE);
        for(SysDictData data:sysDictData){
            XlkjHouse house = new XlkjHouse();
            house.setHouseType(data.getDictValue());
            List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(house);
            HouseServeyVo houseServeyVo = new HouseServeyVo();
            houseServeyVo.setName(data.getDictLabel());
            houseServeyVo.setValue(xlkjHouses.size());
            houseServeyVos.add(houseServeyVo);
        }

        return AjaxResult.success(houseServeyVos);
    }

    /**
     * 单位数据列表
     */
    @GetMapping("/getCompanyList")
    public AjaxResult getCompanyList(){
        List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(new XlkjCompany());
        for(XlkjCompany company:xlkjCompanies){
            //大数据字段更改（因为使用不了name)
            company.setNa(company.getName());
            //字典值翻译
            company.setCompanyType(DictUtils.getDictLabel(DictStrUtils.COMPANY_TYPE,company.getCompanyType()));
            company.setEconomicsType(DictUtils.getDictLabel(DictStrUtils.ECONOMICS_TYPE,company.getEconomicsType()));
        }
        return AjaxResult.success(xlkjCompanies);
    }

    /**
     * 用户随手拍类型统计
     */
    @GetMapping("/getClapCount")
    public AjaxResult getClapCount(){
        List<HouseServeyVo> serveyVos = new ArrayList<>();

        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(DictStrUtils.CLAP_TYPE);
        for(SysDictData data:sysDictData){
            XlkjClap clap = new XlkjClap();
            clap.setType(data.getDictValue());
            List<XlkjClap> xlkjClaps = clapService.selectXlkjClapList(clap);
            HouseServeyVo houseServeyVo = new HouseServeyVo();
            houseServeyVo.setName(data.getDictLabel());
            houseServeyVo.setValue(xlkjClaps.size());
            serveyVos.add(houseServeyVo);
        }

        return AjaxResult.success(serveyVos);
    }

    /**
     * 全部上报事件列表数据接口
     */
    @GetMapping("/getIniSucc")
    public AjaxResult getIniSucc(){
        List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(new XlkjIni());
        for(XlkjIni ini:xlkjInis){
            ini.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,ini.getType()));
            ini.setRiskType(DictUtils.getDictLabel(DictStrUtils.RISK2_TYPE,ini.getRiskType()));
            ini.setAssist(DictUtils.getDictLabel(DictStrUtils.SYS_YES_NO,ini.getAssist()));
            ini.setDepartmentId(sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId())).getDeptName());
        }
        return AjaxResult.success(xlkjInis);
    }

    /**
     * 全部审批完成事件列表数据接口
     */
    @GetMapping("/getIni")
    public AjaxResult getIni(){
        XlkjIni xlkjIni = new XlkjIni();
        xlkjIni.setStatus("2");
        List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(xlkjIni);
        for(XlkjIni ini:xlkjInis){
            ini.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,ini.getType()));
            ini.setRiskType(DictUtils.getDictLabel(DictStrUtils.RISK2_TYPE,ini.getRiskType()));
            ini.setAssist(DictUtils.getDictLabel(DictStrUtils.SYS_YES_NO,ini.getAssist()));
            ini.setDepartmentId(sysDeptService.selectDeptById(Long.valueOf(ini.getDepartmentId())).getDeptName());
        }
        return AjaxResult.success(xlkjInis);
    }

}
